#ifndef THINK_THINK_RESPONSE_XML_H
#define THINK_THINK_RESPONSE_XML_H

static char* think_response_xml =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\response;\n"
	"\n"
	"use think\\Collection;\n"
	"use think\\Model;\n"
	"use think\\Response;\n"
	"\n"
	"class Xml extends Response\n"
	"{\n"
	"    // 输出参数\n"
	"    protected $options = [\n"
	"        // 根节点名\n"
	"        \'root_node\' => \'think\',\n"
	"        // 根节点属性\n"
	"        \'root_attr\' => \'\',\n"
	"        //数字索引的子节点名\n"
	"        \'item_node\' => \'item\',\n"
	"        // 数字索引子节点key转换的属性名\n"
	"        \'item_key\'  => \'id\',\n"
	"        // 数据编码\n"
	"        \'encoding\'  => \'utf-8\',\n"
	"    ];\n"
	"\n"
	"    protected $contentType = \'text/xml\';\n"
	"\n"
	"    /**\n"
	"     * 处理数据\n"
	"     * @access protected\n"
	"     * @param mixed $data 要处理的数据\n"
	"     * @return mixed\n"
	"     */\n"
	"    protected function output($data)\n"
	"    {\n"
	"        // XML数据转换\n"
	"        return $this->xmlEncode($data, $this->options[\'root_node\'], $this->options[\'item_node\'], $this->options[\'root_attr\'], $this->options[\'item_key\'], $this->options[\'encoding\']);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * XML编码\n"
	"     * @param mixed $data 数据\n"
	"     * @param string $root 根节点名\n"
	"     * @param string $item 数字索引的子节点名\n"
	"     * @param string $attr 根节点属性\n"
	"     * @param string $id   数字索引子节点key转换的属性名\n"
	"     * @param string $encoding 数据编码\n"
	"     * @return string\n"
	"     */\n"
	"    protected function xmlEncode($data, $root, $item, $attr, $id, $encoding)\n"
	"    {\n"
	"        if (is_array($attr)) {\n"
	"            $array = [];\n"
	"            foreach ($attr as $key => $value) {\n"
	"                $array[] = \"{$key}=\\\"{$value}\\\"\";\n"
	"            }\n"
	"            $attr = implode(\' \', $array);\n"
	"        }\n"
	"        $attr = trim($attr);\n"
	"        $attr = empty($attr) ? \'\' : \" {$attr}\";\n"
	"        $xml  = \"<?xml version=\\\"1.0\\\" encoding=\\\"{$encoding}\\\"?>\";\n"
	"        $xml .= \"<{$root}{$attr}>\";\n"
	"        $xml .= $this->dataToXml($data, $item, $id);\n"
	"        $xml .= \"</{$root}>\";\n"
	"        return $xml;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 数据XML编码\n"
	"     * @param mixed  $data 数据\n"
	"     * @param string $item 数字索引时的节点名称\n"
	"     * @param string $id   数字索引key转换为的属性名\n"
	"     * @return string\n"
	"     */\n"
	"    protected function dataToXml($data, $item, $id)\n"
	"    {\n"
	"        $xml = $attr = \'\';\n"
	"\n"
	"        if ($data instanceof Collection || $data instanceof Model) {\n"
	"            $data = $data->toArray();\n"
	"        }\n"
	"\n"
	"        foreach ($data as $key => $val) {\n"
	"            if (is_numeric($key)) {\n"
	"                $id && $attr = \" {$id}=\\\"{$key}\\\"\";\n"
	"                $key         = $item;\n"
	"            }\n"
	"            $xml .= \"<{$key}{$attr}>\";\n"
	"            $xml .= (is_array($val) || is_object($val)) ? $this->dataToXml($val, $item, $id) : $val;\n"
	"            $xml .= \"</{$key}>\";\n"
	"        }\n"
	"        return $xml;\n"
	"    }\n"
	"}\n"
	"\n"
;
#endif